package com.guomao.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guomao.clients.UserFeign;
import com.guomao.entity.Order;
import com.guomao.entity.User;
import com.guomao.mapper.OrderMapper;
import com.guomao.service.OrderService;
//import com.guomao.service.UserService;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.net.http.HttpClient;
import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private RestTemplate restTemplate;

    //从ioc容器中装配userService对象
//    @Autowired
//    private UserService userService;

/*    @Override
    public Order findById(Long id) {
        //1 调用继承过来的getById方法查询订单
        try {
            Order order = getById(id);
            CloseableHttpClient client = HttpClients.createDefault();

            HttpGet httpGet = new HttpGet("http://localhost:8081/user/getUser/" + order.getUserId());

            CloseableHttpResponse response = client.execute(httpGet);

            HttpEntity entity = response.getEntity();

            //{"id":"1","username":"赵一","sex":"男"}
            String json = EntityUtils.toString(entity);

            User user = JSON.parseObject(json, User.class);
            //2 调用userService查询订单的用户信息
//        User user = userService.getById(order.getUserId());
            order.setUser(user);
            return order;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }*/

/*    @Override
    public Order findById(Long id) {
        //根据id查询订单数据
        Order order = getById(id);
        //根据userId查询用户
        //方式二：使用RestTemplate
        User user = restTemplate.getForObject("http://localhost:8081/user/getUser/" +
                order.getUserId(), User.class);
        order.setUser(user);
        return order;
    }*/

/*    @Override
    public Order findById(Long id) {
        //根据id查询订单数据
        Order order = getById(id);
        List<ServiceInstance> list =
                discoveryClient.getInstances("DISTRIBUTED-USER");
//                discoveryClient.getInstances("Distributed-User");
        ServiceInstance instance = list.get(0);
        String host = instance.getHost();
        int port = instance.getPort();
        String url = "http://" + host + ":" + port + "user/getUser/" + order.getUserId();
        User user = restTemplate.getForObject(url, User.class);
        order.setUser(user);
        return order;
    }*/

    @Autowired
    private UserFeign userFeign;

    @Override
    public Order findById(Long id) {
        //根据id查询订单数据
        Order order = getById(id);
        System.out.println(userFeign.getClass().getName());
        User user = userFeign.findByID(order.getUserId());
        order.setUser(user);
        return order;
    }
}